﻿using System;
using System.Collections.Generic;
using System.Linq;
using DCS.Business.Model;
using DCS.DataProvider;
using CarolLib;
using System.Configuration;
using System.Text.RegularExpressions;

namespace DCS.Business.Manager
{
    public class PRLMessageManager
    {
        private IDataProvider provider;
        static log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public PRLMessageManager(IDataProvider p)
        {
            this.provider = p;
        }

        public void SaveEntities(List<PRLMessage> results)
        {
            foreach (var item in results)
            {
                try
                {
                    provider.CreatePRLMessage(item);
                }
                catch (Exception e)
                {
                    Logger.Error(e);
                }
            }
        }

        #region Publish

        private static readonly string KEY_GetFQPassengers = "PRL-{0}-{1}{2}-{3}-{4}{5}";
        protected int CacheSeconds 
        {
            get
            {
                if (ConfigurationManager.AppSettings["CacheSeconds"] != string.Empty)
                {
                    return int.Parse(ConfigurationManager.AppSettings["CacheSeconds"]);
                }
                else
                {
                    return 1;
                }
            }
        }

        public List<PRLMessage> GetPassengersByOpt(string option, string carrier, string flightNo, DateTime pdate, string dept, string arr)
        {
            List<PRLMessage> result = null;
            if (!CacheHelper.TryGet<List<PRLMessage>>(GetCacheKey(option,carrier, flightNo, pdate, dept, arr), out result))
            {
                try
                {
                    string sql = string.Format(ConfigurationManager.AppSettings[option], carrier, flightNo, pdate.ToShortDateString(), dept, arr);
                    result = provider.QueryModelList<PRLMessage>(sql);

                    result.ForEach(p => p.PSM = Regex.Replace(p.PSM, "[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]", "*"));

                    CacheHelper.Insert(GetCacheKey(option,carrier, flightNo, pdate, dept, arr), result, CacheSeconds);
                }
                catch (Exception e)
                {
                    Logger.Error("Error During" + option, e);
                }
            }
            return result;
        }

        private static string GetCacheKey(string option, string carrier, string flightNo, DateTime pdate, string dept, string arr)
        {
            return string.Format(KEY_GetFQPassengers, option, carrier, flightNo, pdate.ToShortDateString(), dept, arr);
        }

        public List<PRLMessage> QueryTripHistory(string whereString)
        {
            return provider.QueryTripHistory(whereString);
        }

        #endregion

        #region SSRCode

        public Dictionary<string, SSRCode> GetSPMLs()
        {
            return provider.GetSPMLs();
        }

        public Dictionary<string, SSRCode> GetSSRs()
        {
            return provider.GetSSRs();
        }

        #endregion

    }
}
